<?php
namespace App\Model;

class Access_backModel extends \App\Model\CommonModel{
    protected $tableName = 'szh_access_back';
    protected $primaryKey = 'itemid';

    //后台权限
	public function set_auth($userid,$post){
		isset($post['access']) or $post['access']=[];
		self::create()->where(['userid'=>$userid,'moduleid'=>$post['moduleid']])->destroy();
		
		foreach($post['access'] as $controller=>$actions){
			foreach($actions as $action){
				self::create(['userid'=>$userid,'moduleid'=>$post['moduleid'],'module'=>$post['module'],'controller'=>$controller,'action'=>$action])->save();
			}
		}
		return true;
	}
	public function get_auth($userid,$moduleid){
		$data=self::create()->where(['userid'=>$userid,'moduleid'=>$moduleid])->select();
		$return=[];
		foreach($data as $k=>$v){
			$return[$v['controller']][]=$v['action'];
		}
		return $return;
	}
}